// views/index/shop/certificate/paypage/third/index.js
var BASE_URL = getApp().globalData.BASE_URL
var util = require('../../../../../../utils/util');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dingdan: '',
    id: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id : options.id
    })
    var userinfo = wx.getStorageSync("USER")
    var _this = this
    wx.request({
      url: `${BASE_URL}/pay/index`,
      method: "POST",
      data: {
        // id: 18449,
        id: options.id,
        type: 'xiaochengxv',
        uid: userinfo.id
      },
      header: {
        'Content-Type': 'application/json' //GET方式是这个
      },
      success(res) {
        console.log(res)
        res.data.list.time = util.formatTimeTwo(res.data.list.time, 'Y-M-D h:m:s');
        _this.setData({
          dingdan: res.data.list
        })
      }
    })
  },



  gopay: function () {
    var opid = wx.getStorageSync("openid")
    this.generateOrder(opid)
  },
  generateOrder: function (openid) {
    var that = this;
    wx.request({
      url: `${BASE_URL}/static/beecloud-php/demo/wx/wx.mini.php`,
      data: {
        type: 'pay',
        openid: openid,
        or_type: that.data.dingdan.type,
        openid: openid,
        amount: that.data.dingdan.amount,
        name: that.data.dingdan.name,
        id: that.data.id,
        title: that.data.dingdan.title
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success: function (res) {
        console.log(res);
        if (res.data.resultCode != 0) {
          that.showInfo(res.data.errMsg);
          return;
        }
        that.pay(res.data.params);
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  pay: function (param) {
    var that = this;
    wx.requestPayment({
      timeStamp: param.timestamp,
      nonceStr: param.nonce_str,
      package: param.package,
      signType: param.sign_type,
      paySign: param.pay_sign,
      success: function (res) {
        wx.reLaunch({
          url: `../../../../../mine/myorder/index?id=${that.data.id}`
        })
        // success
        console.log(res);
        that.showInfo('支付成功');
      },
      fail: function (res) {
        // fail
        console.log(res);
        var strMsg = res.errMsg;
        if (res.err_desc) {
          strMsg += ', ' + res.err_desc;
        }
        that.showInfo('您取消了支付');
      },
      complete: function () {
        // complete
        console.log("pay complete");
      }
    });
  },
  showInfo: function (msg) { //错误信息提示
    wx.showModal({
      title: '提示',
      showCancel: false,
      content: msg
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})